package com.xkh.common.controller;

import com.pig4cloud.plugin.idempotent.annotation.Idempotent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author xkh
 * @date 2020/11/10 11:07
 */
@RestController
@RequestMapping("util/common")
@Api(tags = "公共模块")
public class CommonController {

    @PostMapping("/idempotent")
    @ApiOperation("接口幂等性")
    @Idempotent(key = "idempotent",expireTime = 10,info = "请勿重复查询")
    public String idempotent(){
        return "success";
    }

    @GetMapping("/insertUserId")
    @ApiOperation("自动组装insertUserId")
    public String insertUserId(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("userId","10000");
        return "自动组装insertUserId";
    }


    @GetMapping("/userId")
    @ApiOperation("自动组装userId")
    public String userId(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object o = session.getAttribute("userId");
        if(ObjectUtils.isNotEmpty(o)){
            return o.toString();
        }
        return null;
    }
}
